<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
	        	var result = "";
				$("#btnSearch").button({
		            icons: {
		                primary: "ui-icon-search"
		            }
		        }).click(function(){
					if(validateEmpty()){
						result = dateDiff($("#startDate").val(), $("#endDate").val(),"/");
			        	if (!result && $("#startDate").val() != "" && $("#endDate").val() != "") {
			        		alert("วันเริ่มต้นต้องน้อยกว่าวันสิ้นสุดกรุณาระบุใหม่");
			        		$("#endDate").val("");
							return false;
			        	} else {
							$("#searchForm").submit();
						}
					} else {
						return false;
					}
		        });
				$("#btnExport").button({
		            icons: {
		                primary: "ui-icon-arrowthickstop-1-s"
		            }
				}).click(function(){
					if(validateEmpty()){
						result = dateDiff($("#startDate").val(), $("#endDate").val(),"/");
			        	if (!result) {
			        		alert("วันเริ่มต้นมากกว่าวันสิ้นสุดกรุณาระบุใหม่");
			        		$("#endDate").val("");
							return false;
			        	} else {
			        		window.location='${pageContext.request.contextPath}/acc6?app=ACC60401&job=export&budgetYear='+$("#budgetYear").val()+'&provinceId='+$("#provinceId").val()+"&startDate="+$("#startDate").val()+"&endDate="+$("#endDate").val()+"&type="+$("#type").val()+"&allocateYear="+$("#allocateYear").val();
			        	}
					}	
				});
				
				$("#budgetYear").change(function(){
					if ($("#budgetYear").val() != "") {
						$("#startDate").val("01/10/"+($("#budgetYear").val()-1));
					}else {
						$("#startDate").val("");
					}
				});
		        thaiDatepicker("endDate","#","1");
			});
			function setSummaly(){
				if(document.getElementById("sumUC1") != null) {
					document.getElementById("sumUC1").innerHTML = document.getElementById("hSumUC1").value;
					document.getElementById("sumUC2").innerHTML = document.getElementById("hSumUC2").value;
					document.getElementById("sumUC3").innerHTML = document.getElementById("hSumUC3").value;
					document.getElementById("sumUC4").innerHTML = document.getElementById("hSumUC4").value;
				}

				if (document.getElementById("sumNoUC1") != null) {
					document.getElementById("sumNoUC1").innerHTML = document.getElementById("hSumNoUC1").value;
					document.getElementById("sumNoUC2").innerHTML = document.getElementById("hSumNoUC2").value;
					document.getElementById("sumNoUC3").innerHTML = document.getElementById("hSumNoUC3").value;
					document.getElementById("sumNoUC4").innerHTML = document.getElementById("hSumNoUC4").value;
				}
			}
		</script>
	</head>
	<body onload="setSummaly();">
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="searchForm" id="searchForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60401"/>
		<input type="hidden" name="job" id="job" value="search"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60401}</b></th>
			</tr>
			<tr>
				<td class="bold">จังหวัด : </td>
				<td>
					<c:if test="${userInfo.areaLevel =='3'}">
						<c:forEach var="data" items="${userInfo.listUserProvice}">
							${data.provinceName}
							<input type="hidden" name="provinceId" id="provinceId" value="${data.provinceId}">
						</c:forEach>
					</c:if>
					<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
						<select name="provinceId" id="provinceId">
							<option value="">เลือกทั้งหมด</option>
							<c:forEach var="data" items="${userInfo.listUserProvice}">
								<c:if test="${data.provinceId == provinceId}" var="provinceSeleted"/>
								<option value="${data.provinceId}" ${provinceSeleted?'selected':''}>${data.provinceName}</option>
							</c:forEach>
						</select>
						<!-- <span id="$provinceId" class="red">*</span> -->
					</c:if>
				</td>
			</tr>
			<tr>
				<td class="bold">ปีที่ออกรายงาน : </td>
				<td>
					<select name="budgetYear" id="budgetYear">
						<option value="">----- เลือก -----</option>
						<c:forEach items="${lsBudgetYear}" var="data">
							<c:if test="${budgetYear == data.BUDGET_YEAR}" var="selected"/>
							<option value="${data.BUDGET_YEAR}" ${selected?'selected':''}>${data.BUDGET_YEAR}</option>
						</c:forEach>
					</select>
					<span id="$budgetYear" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td class="bold">วันที่ออกรายงาน : </td>
				<td>
					<input type="text" name="startDate" id="startDate" readonly="readonly" size="12" maxlength="10" onkeypress="return numberAndSlash(event);" onblur="dateFormat(this);" value="${startDate}"/>
					ถึงวันที่
					<input type="text" name="endDate" id="endDate" size="12" maxlength="10" onkeypress="return numberAndSlash(event);" onblur="dateFormat(this);" value="${endDate}"/>
					<span id="#endDate" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td class="bold">รหัสเงินงบประมาณ : </td>
				<td>
					<select name="allocateYear" id="allocateYear">
						<option value="">----- เลือก -----</option>
						<c:forEach items="${lsBudgetYear}" var="data">
							<c:if test="${allocateYear == data.BUDGET_YEAR}" var="selected"/>
							<option value="${data.BUDGET_YEAR}" ${selected?'selected':''}>${data.BUDGET_YEAR}</option>
						</c:forEach>
					</select>
					<span id="$allocateYear" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td class="bold">ประเภท : </td>
				<td>
					<select name="type" id="type">
						<option value="0" ${'0'== type ?'selected':''}>UC และ ไม่ใช่ UC</option>
						<option value="1" ${'1'== type ?'selected':''}>UC</option>
						<option value="2" ${'2'== type ?'selected':''}>ไม่ใช่ UC</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="center" colspan="2">
					<button type="button" name="btnSearch" id="btnSearch" class="acc67-button"> ค้นหาข้อมูล </button>
					<button type="button" name="btnExport" id="btnExport" class="acc67-button"> Download </button>
				</td>
			</tr>
		</table>
		<br/>
		<c:if test="${!empty listAllocateNoUc || !empty listAllocateUc}">
			<fieldset>
				<legend> รายจ่ายในแต่ละจังหวัด ตามปีงบประมาณ </legend>
				<br/>
					<b class="red"> * รายงานนี้เป็นรายงานที่รวมจำนวนภาษีหัก ณ ที่จ่ายแล้ว</b>
				<br/>
					<b>จังหวัด : ${provinceName}</b>
					<c:if test="${!empty listAllocateUc}">
					<table width="100%" class="acc67-grid ui-widget ui-widget-content" border="0">
						<tr>
							<c:set var="sumUC1" value="0"/>
							<c:set var="sumUC2" value="0"/>
							<c:set var="sumUC3" value="0"/>
							<c:set var="sumUC4" value="0"/>
							<td colspan="2" align="right"> ยอดรวม </td>
							<td align="right" id="sumUC1"></td>
							<td align="right" id="sumUC2"></td>
							<td align="right" id="sumUC3"></td>
							<td align="right" id="sumUC4"></td>
						</tr>
						<tr>
							<th align="center" class="ui-state-default">ชื่อแจ้งจัดสรรเงินให้คู่สัญญา</th>
							<th align="center" class="ui-state-default" width="20%">รหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
							<th align="center" class="ui-state-default" width="7%">ยอดยกมา</th> <!-- B -->
							<th align="center" class="ui-state-default" width="12%">จำนวนเงินที่ได้รับจัดสรร</th><!-- S -->
							<th align="center" class="ui-state-default" width="15%">จำนวนเงินที่จ่ายให้หน่วยบริการ</th><!-- T,C,A -->
							<th align="center" class="ui-state-default" width="7%">คงเหลือ</th> <!--(S+B) - (T,C,A )-->
						</tr>
						<c:forEach items="${listAllocateUc}" var="data" varStatus="x">
							<c:if test="${data.allocateLevel == '1'}">
								<tr class="grid-tr-lvl1">
								<c:set var="sumUC1" value="${sumUC1 + data.sumIncomeAmountTypeB}"/>
								<c:set var="sumUC2" value="${sumUC2 + data.sumIncomeAmountTypeS}"/>
								<c:set var="sumUC3" value="${sumUC3 + data.sumExpenseAmount}"/>
								<c:set var="sumUC4" value="${sumUC4 + ((data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount)}"/>
							</c:if>
							<c:if test="${data.allocateLevel == '2'}">
								<tr class="grid-tr-lvl2">
							</c:if>
							<c:if test="${data.allocateLevel == '3'}">
								<tr class="grid-tr-${x.count % 2}">
							</c:if>
								<td align="left">${data.allocateName}</td>
								<td align="left">${data.allocateCode}</td>
								<c:if test="${empty data.sumIncomeAmountTypeB}">
									<td align="right"> - </td>
								</c:if>
								<c:if test="${!empty data.sumIncomeAmountTypeB}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeB}" />
									</td>
								</c:if>
								<c:if test="${data.sumIncomeAmountTypeS == ''}">
									<td align="right"> 0.00 </td>
								</c:if>
								<c:if test="${data.sumIncomeAmountTypeS != ''}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeS}" />
									</td>
								</c:if>
								<c:if test="${data.sumExpenseAmount == ''}">
									<td align="right"> 0.00 </td>
								</c:if>
								<c:if test="${data.sumExpenseAmount != ''}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumExpenseAmount}" />
									</td>
								</c:if>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${(data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount}" />
								</td>
							</tr>
						</c:forEach>
						<input type="hidden" id="hSumUC1" value="<fmt:formatNumber pattern="#,##0.00" value="${sumUC1}"/>"/>
						<input type="hidden" id="hSumUC2" value="<fmt:formatNumber pattern="#,##0.00" value="${sumUC2}"/>"/>
						<input type="hidden" id="hSumUC3" value="<fmt:formatNumber pattern="#,##0.00" value="${sumUC3}"/>"/>
						<input type="hidden" id="hSumUC4" value="<fmt:formatNumber pattern="#,##0.00" value="${sumUC4}"/>"/>
					</table>
			</c:if>
			<br/>
			<c:if test="${!empty listAllocateNoUc}">
					<!-- No Uc -->
					<table width="100%" class="acc67-grid ui-widget ui-widget-content" border="0">
						<tr>
							<c:set var="sumNoUC1" value="0"/>
							<c:set var="sumNoUC2" value="0"/>
							<c:set var="sumNoUC3" value="0"/>
							<c:set var="sumNoUC4" value="0"/>
							<td colspan="2" align="right"> ยอดรวม </td>
							<td align="right" id="sumNoUC1"></td>
							<td align="right" id="sumNoUC2"></td>
							<td align="right" id="sumNoUC3"></td>
							<td align="right" id="sumNoUC4"></td>
						</tr>
						<tr>
							<th align="center" class="ui-state-default">ชื่อแจ้งจัดสรรเงินให้คู่สัญญา</th>
							<th align="center" class="ui-state-default" width="20%">รหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
							<th align="center" class="ui-state-default" width="7%">ยอดยกมา</th> <!-- B -->
							<th align="center" class="ui-state-default" width="12%">จำนวนเงินที่ได้รับจัดสรร</th><!-- S -->
							<th align="center" class="ui-state-default" width="15%">จำนวนเงินที่จ่ายให้หน่วยบริการ</th><!-- T,C,A -->
							<th align="center" class="ui-state-default" width="7%">คงเหลือ</th> <!--(S+B) - (T,C,A )-->
						</tr>
						<c:forEach items="${listAllocateNoUc}" var="data" varStatus="x">
							<c:if test="${data.allocateLevel == '1'}">
								<tr class="grid-tr-lvl1">
								<c:set var="sumNoUC1" value="${sumNoUC1 + data.sumIncomeAmountTypeB}"/>
								<c:set var="sumNoUC2" value="${sumNoUC2 + data.sumIncomeAmountTypeS}"/>
								<c:set var="sumNoUC3" value="${sumNoUC3 + data.sumExpenseAmount}"/>
								<c:set var="sumNoUC4" value="${sumNoUC4 + ((data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount)}"/>
							
							</c:if>
							<c:if test="${data.allocateLevel == '2'}">
								<tr class="grid-tr-lvl2">
							</c:if>
							<c:if test="${data.allocateLevel == '3'}">
								<tr class="grid-tr-${x.count % 2}">
							</c:if>
								<td align="left">${data.allocateName}</td>
								<td align="left">${data.allocateCode}</td>
								<c:if test="${empty data.sumIncomeAmountTypeB}">
									<td align="right"> - </td>
								</c:if>
								<c:if test="${!empty data.sumIncomeAmountTypeB}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeB}" />
									</td>
								</c:if>
								<c:if test="${data.sumIncomeAmountTypeS == ''}">
									<td align="right"> 0.00 </td>
								</c:if>
								<c:if test="${data.sumIncomeAmountTypeS != ''}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeS}" />
									</td>
								</c:if>
								<c:if test="${data.sumExpenseAmount == ''}">
									<td align="right"> 0.00 </td>
								</c:if>
								<c:if test="${data.sumExpenseAmount != ''}">
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${data.sumExpenseAmount}" />
									</td>
								</c:if>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${(data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount}" />
								</td>
							</tr>
						</c:forEach>
						<input type="hidden" id="hSumNoUC1" value="<fmt:formatNumber pattern="#,##0.00" value="${sumNoUC1}"/>"/>
						<input type="hidden" id="hSumNoUC2" value="<fmt:formatNumber pattern="#,##0.00" value="${sumNoUC2}"/>"/>
						<input type="hidden" id="hSumNoUC3" value="<fmt:formatNumber pattern="#,##0.00" value="${sumNoUC3}"/>"/>
						<input type="hidden" id="hSumNoUC4" value="<fmt:formatNumber pattern="#,##0.00" value="${sumNoUC4}"/>"/>
					</table>
				</c:if>
				</fieldset>
			</c:if>
		</form>
	</body>
</html>